package org.hgvc.webapp.ets.jsf.beans.agent;
 
import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.Agent;
import org.hgvc.webapp.ets.bo.Agreement;
import org.hgvc.webapp.ets.jsf.beans.admin.AdminAgentMgmtBean;
import org.hgvc.webapp.ets.spring.service.AgreementService;
import org.hgvc.webapp.ets.spring.service.MemberService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;

public class AgentAgreementTransferBean extends AdminAgentMgmtBean {
	public final static String NAV_LIST = "agentAgreementTransfer";
	public static final String BEAN_NAME = "agentAgreementTransferBean";
	private Agent agent;
	private Agent toAgent;
	private AgreementService agreementService;
	private MemberService memberService;
	private List<Agreement> agreements;
	private boolean search;
	
	public void iniBean() throws BaseException{
		toAgent = new Agent();
		search = false;
		agreements = memberService.getActiveAgreementsByAgent(agent.getAgentId());
	}
	
	public boolean isSearch() {
		return search;
	}

	public void setSearch(boolean search) {
		this.search = search;
	}

	public String transferAction() throws Exception{
		try{
			
			memberService.saveAgentAgreementTransfer(agent,toAgent, agreements,getThisUser());
			
		} catch (Exception be) {
			initErrorBean(new BaseException(be));
		}
		return navListAction();
	}
	
	@Override
	public String searchAction(){
		search = true;
		return NAV_LIST.concat("_"+getRoleName());
	}
	public void selectToAgent(ActionEvent actionEvent) throws Exception{
		UIParameter param = (UIParameter)actionEvent.getComponent().findComponent("toAgentId");
		String id = (String)param.getValue();
		toAgent = (Agent)commonDao.getObject(Agent.class, id);
	}
	
	public String navAgreementTransfer(){
		search = false;
		return AgentAgreementTransferBean.NAV_LIST.concat("_"+getRoleName());
	}
	
	public void setAgreementService(AgreementService agreementService) {
		this.agreementService = agreementService;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public AgentAgreementTransferBean() {
	}

	public Agent getAgent() {
		return agent;
	}

	public void setAgent(Agent agent) {
		this.agent = agent;
	}

	public Agent getToAgent() {
		return toAgent;
	}

	public void setToAgent(Agent toAgent) {
		this.toAgent = toAgent;
	}

	public List<Agreement> getAgreements() {
		return agreements;
	}

	public void setAgreements(List<Agreement> agreements) {
		this.agreements = agreements;
	}

	public AgreementService getAgreementService() {
		return agreementService;
	}
	
}
